# Description:
#   libjpeg-turbo is a drop in replacement for jpeglib optimized with SIMD.

# Copied from https://github.com/tensorflow/tensorflow/blob/master/third_party/jpeg/jpeg.BUILD
# and modified to work with Bazel platforms

load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
load("@rules_license//rules:license.bzl", "license")

package(
    default_applicable_licenses = [":license"],
)

exports_files([
    "LICENSE.md",
])

license(
    name = "license",
    license_kinds = [
        "@rules_license//licenses/spdx:IJG",
        "@rules_license//licenses/spdx:Zlib",
        "@rules_license//licenses/spdx:BSD-3-Clause-Modification",
    ],
    license_text = "LICENSE.md",
)


WIN_COPTS = [
    "/Ox",
    "-DWITH_SIMD",
    "-wd4996",
]

libjpegturbo_copts = select({
    ":android": [
        "-O3",
        "-fPIC",
        "-w",
    ],
    ":windows": WIN_COPTS,
    "//conditions:default": [
        "-O3",
        "-w",
    ],
}) + select({
    ":armeabi-v7a": [
        "-D__ARM_NEON__",
        "-DNEON_INTRINSICS",
        "-march=armv7-a",
        "-mfpu=neon",
        "-mfloat-abi=softfp",
        "-fprefetch-loop-arrays",
    ],
    ":arm64-v8a": [
        "-DNEON_INTRINSICS",
    ],
    ":linux_ppc64le": [
        "-mcpu=power8",
        "-mtune=power8",
    ],
    "//conditions:default": [],
})

alias(
    name = "libjpeg_turbo",
    actual = ":jpeg",
    visibility = ["//visibility:public"],
)

cc_library(
    name = "jpeg",
    srcs = [
        "jversion.h",
        "src/jaricom.c",
        "src/jcapimin.c",
        "src/jcarith.c",
        "src/jchuff.c",
        "src/jcicc.c",
        "src/jcinit.c",
        "src/jclhuff.c",
        "src/jcmarker.c",
        "src/jcmaster.c",
        "src/jcmaster.h",
        "src/jcomapi.c",
        "src/jcparam.c",
        "src/jcphuff.c",
        "src/jctrans.c",
        "src/jdapimin.c",
        "src/jdarith.c",
        "src/jdatadst.c",
        "src/jdatasrc.c",
        "src/jdhuff.c",
        "src/jdhuff.h",
        "src/jdicc.c",
        "src/jdinput.c",
        "src/jdlhuff.c",
        "src/jdmainct.h",
        "src/jdmarker.c",
        "src/jdmaster.c",
        "src/jdmaster.h",
        "src/jdphuff.c",
        "src/jdtrans.c",
        "src/jerror.c",
        "src/jfdctflt.c",
        "src/jlossls.h",
        "src/jmemmgr.c",
        "src/jmemnobs.c",
        "src/jmemsys.h",
        "src/jpeg_nbits.c",
        "src/jpeg_nbits.h",
        "src/jpegapicomp.h",
        "src/wrapper/jcapistd-12.c",
        "src/wrapper/jcapistd-16.c",
        "src/wrapper/jcapistd-8.c",
        "src/wrapper/jccoefct-12.c",
        "src/wrapper/jccoefct-8.c",
        "src/wrapper/jccolor-12.c",
        "src/wrapper/jccolor-16.c",
        "src/wrapper/jccolor-8.c",
        "src/wrapper/jcdctmgr-12.c",
        "src/wrapper/jcdctmgr-8.c",
        "src/wrapper/jcdiffct-12.c",
        "src/wrapper/jcdiffct-16.c",
        "src/wrapper/jcdiffct-8.c",
        "src/wrapper/jclossls-12.c",
        "src/wrapper/jclossls-16.c",
        "src/wrapper/jclossls-8.c",
        "src/wrapper/jcmainct-12.c",
        "src/wrapper/jcmainct-16.c",
        "src/wrapper/jcmainct-8.c",
        "src/wrapper/jcprepct-12.c",
        "src/wrapper/jcprepct-16.c",
        "src/wrapper/jcprepct-8.c",
        "src/wrapper/jcsample-12.c",
        "src/wrapper/jcsample-16.c",
        "src/wrapper/jcsample-8.c",
        "src/wrapper/jdapistd-12.c",
        "src/wrapper/jdapistd-16.c",
        "src/wrapper/jdapistd-8.c",
        "src/wrapper/jdcoefct-12.c",
        "src/wrapper/jdcoefct-8.c",
        "src/wrapper/jdcolor-12.c",
        "src/wrapper/jdcolor-16.c",
        "src/wrapper/jdcolor-8.c",
        "src/wrapper/jddctmgr-12.c",
        "src/wrapper/jddctmgr-8.c",
        "src/wrapper/jddiffct-12.c",
        "src/wrapper/jddiffct-16.c",
        "src/wrapper/jddiffct-8.c",
        "src/wrapper/jdlossls-12.c",
        "src/wrapper/jdlossls-16.c",
        "src/wrapper/jdlossls-8.c",
        "src/wrapper/jdmainct-12.c",
        "src/wrapper/jdmainct-16.c",
        "src/wrapper/jdmainct-8.c",
        "src/wrapper/jdmerge-12.c",
        "src/wrapper/jdmerge-8.c",
        "src/wrapper/jdpostct-12.c",
        "src/wrapper/jdpostct-16.c",
        "src/wrapper/jdpostct-8.c",
        "src/wrapper/jdsample-12.c",
        "src/wrapper/jdsample-16.c",
        "src/wrapper/jdsample-8.c",
        "src/wrapper/jfdctfst-12.c",
        "src/wrapper/jfdctfst-8.c",
        "src/wrapper/jfdctint-12.c",
        "src/wrapper/jfdctint-8.c",
        "src/wrapper/jidctflt-12.c",
        "src/wrapper/jidctflt-8.c",
        "src/wrapper/jidctfst-12.c",
        "src/wrapper/jidctfst-8.c",
        "src/wrapper/jidctint-12.c",
        "src/wrapper/jidctint-8.c",
        "src/wrapper/jidctred-12.c",
        "src/wrapper/jidctred-8.c",
        "src/wrapper/jquant1-12.c",
        "src/wrapper/jquant1-8.c",
        "src/wrapper/jquant2-12.c",
        "src/wrapper/jquant2-8.c",
        "src/wrapper/jutils-12.c",
        "src/wrapper/jutils-16.c",
        "src/wrapper/jutils-8.c",
    ],
    hdrs = [
        "src/jcapistd.c",  # should have been named .inc
        "src/jccoefct.c",  # should have been named .inc
        "src/jccolext.c",  # should have been named .inc
        "src/jccolor.c",  # should have been named .inc
        "src/jcdctmgr.c",  # should have been named .inc
        "src/jcdiffct.c",  # should have been named .inc
        "src/jclossls.c",  # should have been named .inc
        "src/jcmainct.c",  # should have been named .inc
        "src/jcprepct.c",  # should have been named .inc
        "src/jcsample.c",  # should have been named .inc
        "src/jdapistd.c",  # should have been named .inc
        "src/jdcoefct.c",  # should have been named .inc
        "src/jdcoefct.h",
        "src/jdcol565.c",  # should have been named .inc
        "src/jdcolext.c",  # should have been named .inc
        "src/jdcolor.c",  # should have been named .inc
        "src/jddctmgr.c",  # should have been named .inc
        "src/jddiffct.c",  # should have been named .inc
        "src/jdlossls.c",  # should have been named .inc
        "src/jdmainct.c",  # should have been named .inc
        "src/jdmerge.c",  # should have been named .inc
        "src/jdmerge.h",
        "src/jdmrg565.c",  # should have been named .inc
        "src/jdmrgext.c",  # should have been named .inc
        "src/jdpostct.c",  # should have been named .inc
        "src/jdsample.c",  # should have been named .inc
        "src/jdsample.h",
        "src/jerror.h",
        "src/jfdctfst.c",  # should have been named .inc
        "src/jfdctint.c",  # should have been named .inc
        "src/jidctflt.c",  # should have been named .inc
        "src/jidctfst.c",  # should have been named .inc
        "src/jidctint.c",  # should have been named .inc
        "src/jidctred.c",  # should have been named .inc
        "src/jmorecfg.h",
        "src/jpegint.h",
        "src/jpeglib.h",
        "src/jquant1.c",  # should have been named .inc
        "src/jquant2.c",  # should have been named .inc
        "src/jsamplecomp.h",
        "src/jstdhuff.c",  # should have been named .inc
        "src/jutils.c",  # should have been named .inc
    ],
    copts = libjpegturbo_copts,
    visibility = ["//visibility:public"],
    deps = select({
        ":nosimd": [":simd_none"],
        ":k8": [":simd_x86_64"],
        ":armeabi-v7a": [":simd_armv7a"],
        ":arm64-v8a": [":simd_armv8a"],
        ":linux_ppc64le": [":simd_altivec"],
        ":windows": [":simd_win_x86_64"],
        "//conditions:default": [":simd_none"],
    }),
    strip_include_prefix = "src",
)

cc_library(
    name = "simd_altivec",
    srcs = [
        "src/jchuff.h",
        "jconfig.h",
        "jconfigint.h",
        "src/jdct.h",
        "src/jerror.h",
        "src/jinclude.h",
        "src/jmorecfg.h",
        "src/jpegint.h",
        "src/jpeglib.h",
        "src/jsimd.h",
        "src/jsimddct.h",
        "simd/jsimd.h",
        "simd/powerpc/jccolor-altivec.c",
        "simd/powerpc/jcgray-altivec.c",
        "simd/powerpc/jcsample-altivec.c",
        "simd/powerpc/jdcolor-altivec.c",
        "simd/powerpc/jdmerge-altivec.c",
        "simd/powerpc/jdsample-altivec.c",
        "simd/powerpc/jfdctfst-altivec.c",
        "simd/powerpc/jfdctint-altivec.c",
        "simd/powerpc/jidctfst-altivec.c",
        "simd/powerpc/jidctint-altivec.c",
        "simd/powerpc/jquanti-altivec.c",
        "simd/powerpc/jsimd.c",
    ],
    hdrs = [
        "simd/powerpc/jccolext-altivec.c",
        "simd/powerpc/jcgryext-altivec.c",
        "simd/powerpc/jcsample.h",
        "simd/powerpc/jdcolext-altivec.c",
        "simd/powerpc/jdmrgext-altivec.c",
        "simd/powerpc/jsimd_altivec.h",
    ],
    copts = libjpegturbo_copts,
)

SRCS_SIMD_COMMON = [
    "jconfig.h",
    "jconfigint.h",
    "simd/jsimd.h",
    "src/jchuff.h",
    "src/jdct.h",
    "src/jerror.h",
    "src/jinclude.h",
    "src/jmorecfg.h",
    "src/jpegint.h",
    "src/jpeglib.h",
    "src/jsamplecomp.h",
    "src/jsimd.h",
    "src/jsimddct.h",
]

cc_library(
    name = "simd_x86_64",
    srcs = [
        "simd/x86_64/jccolor-avx2.o",
        "simd/x86_64/jccolor-sse2.o",
        "simd/x86_64/jcgray-avx2.o",
        "simd/x86_64/jcgray-sse2.o",
        "simd/x86_64/jchuff-sse2.o",
        "simd/x86_64/jcphuff-sse2.o",
        "simd/x86_64/jcsample-avx2.o",
        "simd/x86_64/jcsample-sse2.o",
        "simd/x86_64/jdcolor-avx2.o",
        "simd/x86_64/jdcolor-sse2.o",
        "simd/x86_64/jdmerge-avx2.o",
        "simd/x86_64/jdmerge-sse2.o",
        "simd/x86_64/jdsample-avx2.o",
        "simd/x86_64/jdsample-sse2.o",
        "simd/x86_64/jfdctflt-sse.o",
        "simd/x86_64/jfdctfst-sse2.o",
        "simd/x86_64/jfdctint-avx2.o",
        "simd/x86_64/jfdctint-sse2.o",
        "simd/x86_64/jidctflt-sse2.o",
        "simd/x86_64/jidctfst-sse2.o",
        "simd/x86_64/jidctint-avx2.o",
        "simd/x86_64/jidctint-sse2.o",
        "simd/x86_64/jidctred-sse2.o",
        "simd/x86_64/jquantf-sse2.o",
        "simd/x86_64/jquanti-avx2.o",
        "simd/x86_64/jquanti-sse2.o",
        "simd/x86_64/jsimd.c",
        "simd/x86_64/jsimdcpu.o",
    ] + SRCS_SIMD_COMMON,
    copts = libjpegturbo_copts,
    linkstatic = True,
)

genrule(
    name = "simd_x86_64_assemblage23",
    srcs = [
        "jconfig.h",
        "jconfigint.h",
        "simd/x86_64/jccolext-avx2.asm",
        "simd/x86_64/jccolext-sse2.asm",
        "simd/x86_64/jccolor-avx2.asm",
        "simd/x86_64/jccolor-sse2.asm",
        "simd/x86_64/jcgray-avx2.asm",
        "simd/x86_64/jcgray-sse2.asm",
        "simd/x86_64/jcgryext-avx2.asm",
        "simd/x86_64/jcgryext-sse2.asm",
        "simd/x86_64/jchuff-sse2.asm",
        "simd/x86_64/jcphuff-sse2.asm",
        "simd/x86_64/jcsample-avx2.asm",
        "simd/x86_64/jcsample-sse2.asm",
        "simd/x86_64/jdcolext-avx2.asm",
        "simd/x86_64/jdcolext-sse2.asm",
        "simd/x86_64/jdcolor-avx2.asm",
        "simd/x86_64/jdcolor-sse2.asm",
        "simd/x86_64/jdmerge-avx2.asm",
        "simd/x86_64/jdmerge-sse2.asm",
        "simd/x86_64/jdmrgext-avx2.asm",
        "simd/x86_64/jdmrgext-sse2.asm",
        "simd/x86_64/jdsample-avx2.asm",
        "simd/x86_64/jdsample-sse2.asm",
        "simd/x86_64/jfdctflt-sse.asm",
        "simd/x86_64/jfdctfst-sse2.asm",
        "simd/x86_64/jfdctint-avx2.asm",
        "simd/x86_64/jfdctint-sse2.asm",
        "simd/x86_64/jidctflt-sse2.asm",
        "simd/x86_64/jidctfst-sse2.asm",
        "simd/x86_64/jidctint-avx2.asm",
        "simd/x86_64/jidctint-sse2.asm",
        "simd/x86_64/jidctred-sse2.asm",
        "simd/x86_64/jquantf-sse2.asm",
        "simd/x86_64/jquanti-avx2.asm",
        "simd/x86_64/jquanti-sse2.asm",
        "simd/x86_64/jsimdcpu.asm",
        "simd/nasm/jcolsamp.inc",
        "simd/nasm/jdct.inc",
        "simd/nasm/jsimdcfg.inc",
        "simd/nasm/jsimdcfg.inc.h",
        "simd/nasm/jsimdext.inc",
    ],
    outs = [
        "simd/x86_64/jccolor-avx2.o",
        "simd/x86_64/jccolor-sse2.o",
        "simd/x86_64/jcgray-avx2.o",
        "simd/x86_64/jcgray-sse2.o",
        "simd/x86_64/jchuff-sse2.o",
        "simd/x86_64/jcphuff-sse2.o",
        "simd/x86_64/jcsample-avx2.o",
        "simd/x86_64/jcsample-sse2.o",
        "simd/x86_64/jdcolor-avx2.o",
        "simd/x86_64/jdcolor-sse2.o",
        "simd/x86_64/jdmerge-avx2.o",
        "simd/x86_64/jdmerge-sse2.o",
        "simd/x86_64/jdsample-avx2.o",
        "simd/x86_64/jdsample-sse2.o",
        "simd/x86_64/jfdctflt-sse.o",
        "simd/x86_64/jfdctfst-sse2.o",
        "simd/x86_64/jfdctint-avx2.o",
        "simd/x86_64/jfdctint-sse2.o",
        "simd/x86_64/jidctflt-sse2.o",
        "simd/x86_64/jidctfst-sse2.o",
        "simd/x86_64/jidctint-avx2.o",
        "simd/x86_64/jidctint-sse2.o",
        "simd/x86_64/jidctred-sse2.o",
        "simd/x86_64/jquantf-sse2.o",
        "simd/x86_64/jquanti-avx2.o",
        "simd/x86_64/jquanti-sse2.o",
        "simd/x86_64/jsimdcpu.o",
    ],
    cmd = "for out in $(OUTS); do\n" +
          "  $(location @nasm//:nasm) -f elf64" +
          "    -DELF -DPIC -D__x86_64__" +
          "    -I $$(dirname $(location jconfig.h))/" +
          "    -I $$(dirname $(location jconfigint.h))/" +
          "    -I $$(dirname $(location simd/nasm/jsimdcfg.inc.h))/" +
          "    -I $$(dirname $(location simd/x86_64/jccolext-sse2.asm))/" +
          "    -o $$out" +
          "    $$(dirname $(location simd/x86_64/jccolext-sse2.asm))/$$(basename $${out%.o}.asm)\n" +
          "done",
    tools = ["@nasm"],
)

expand_template(
    name = "neon-compat_gen",
    out = "simd/arm/neon-compat.h",
    substitutions = {
        "#cmakedefine HAVE_VLD1_S16_X3": "#define HAVE_VLD1_S16_X3",
        "#cmakedefine HAVE_VLD1_U16_X2": "#define HAVE_VLD1_U16_X2",
        "#cmakedefine HAVE_VLD1Q_U8_X4": "#define HAVE_VLD1Q_U8_X4",
    },
    template = "simd/arm/neon-compat.h.in",
)

genrule(
    name = "neon-compat_hdr_src",
    srcs = ["simd/arm/neon-compat.h"],
    outs = ["neon-compat.h"],
    cmd = "cp $(location simd/arm/neon-compat.h) $@",
)

cc_library(
    name = "neon-compat_hdr",
    hdrs = ["neon-compat.h"],
    copts = libjpegturbo_copts,
)

SRCS_SIMD_ARM = [
    "simd/arm/jccolor-neon.c",
    "simd/arm/jcgray-neon.c",
    "simd/arm/jcphuff-neon.c",
    "simd/arm/jcsample-neon.c",
    "simd/arm/jdcolor-neon.c",
    "simd/arm/jdmerge-neon.c",
    "simd/arm/jdsample-neon.c",
    "simd/arm/jfdctfst-neon.c",
    "simd/arm/jfdctint-neon.c",
    "simd/arm/jidctfst-neon.c",
    "simd/arm/jidctint-neon.c",
    "simd/arm/jidctred-neon.c",
    "simd/arm/jquanti-neon.c",
]

# .c files in the following list are used like .h files in that they are
# "#include"-ed in the actual .c files. So, treat them like normal headers, and
# they *should not* be compiled into individual objects.
HDRS_SIMD_ARM = [
    "simd/arm/align.h",
    "simd/arm/jchuff.h",
    "simd/arm/jcgryext-neon.c",
    "simd/arm/jdcolext-neon.c",
    "simd/arm/jdmrgext-neon.c",
]

cc_library(
    name = "simd_armv7a",
    srcs = [
        "simd/arm/aarch32/jchuff-neon.c",
        "simd/arm/aarch32/jsimd.c",
    ] + SRCS_SIMD_COMMON + SRCS_SIMD_ARM,
    hdrs = [
        "simd/arm/aarch32/jccolext-neon.c",
    ] + HDRS_SIMD_ARM,
    copts = libjpegturbo_copts,
    visibility = ["//visibility:private"],
    deps = [":neon-compat_hdr"],
)

cc_library(
    name = "simd_armv8a",
    srcs = [
        "simd/arm/aarch64/jchuff-neon.c",
        "simd/arm/aarch64/jsimd.c",
    ] + SRCS_SIMD_COMMON + SRCS_SIMD_ARM,
    hdrs = [
        "simd/arm/aarch64/jccolext-neon.c",
    ] + HDRS_SIMD_ARM,
    copts = libjpegturbo_copts,
    visibility = ["//visibility:private"],
    deps = [":neon-compat_hdr"],
)

cc_library(
    name = "simd_win_x86_64",
    srcs = [
        "simd/x86_64/jccolor-avx2.obj",
        "simd/x86_64/jccolor-sse2.obj",
        "simd/x86_64/jcgray-avx2.obj",
        "simd/x86_64/jcgray-sse2.obj",
        "simd/x86_64/jchuff-sse2.obj",
        "simd/x86_64/jcphuff-sse2.obj",
        "simd/x86_64/jcsample-avx2.obj",
        "simd/x86_64/jcsample-sse2.obj",
        "simd/x86_64/jdcolor-avx2.obj",
        "simd/x86_64/jdcolor-sse2.obj",
        "simd/x86_64/jdmerge-avx2.obj",
        "simd/x86_64/jdmerge-sse2.obj",
        "simd/x86_64/jdsample-avx2.obj",
        "simd/x86_64/jdsample-sse2.obj",
        "simd/x86_64/jfdctflt-sse.obj",
        "simd/x86_64/jfdctfst-sse2.obj",
        "simd/x86_64/jfdctint-avx2.obj",
        "simd/x86_64/jfdctint-sse2.obj",
        "simd/x86_64/jidctflt-sse2.obj",
        "simd/x86_64/jidctfst-sse2.obj",
        "simd/x86_64/jidctint-avx2.obj",
        "simd/x86_64/jidctint-sse2.obj",
        "simd/x86_64/jidctred-sse2.obj",
        "simd/x86_64/jquantf-sse2.obj",
        "simd/x86_64/jquanti-avx2.obj",
        "simd/x86_64/jquanti-sse2.obj",
        "simd/x86_64/jsimd.c",
        "simd/x86_64/jsimdcpu.obj",
    ] + SRCS_SIMD_COMMON,
    copts = libjpegturbo_copts,
)

genrule(
    name = "simd_win_x86_64_assemble",
    srcs = [
        "jconfig.h",
        "jconfigint.h",
        "simd/x86_64/jccolext-avx2.asm",
        "simd/x86_64/jccolext-sse2.asm",
        "simd/x86_64/jccolor-avx2.asm",
        "simd/x86_64/jccolor-sse2.asm",
        "simd/x86_64/jcgray-avx2.asm",
        "simd/x86_64/jcgray-sse2.asm",
        "simd/x86_64/jcgryext-avx2.asm",
        "simd/x86_64/jcgryext-sse2.asm",
        "simd/x86_64/jchuff-sse2.asm",
        "simd/x86_64/jcphuff-sse2.asm",
        "simd/x86_64/jcsample-avx2.asm",
        "simd/x86_64/jcsample-sse2.asm",
        "simd/x86_64/jdcolext-avx2.asm",
        "simd/x86_64/jdcolext-sse2.asm",
        "simd/x86_64/jdcolor-avx2.asm",
        "simd/x86_64/jdcolor-sse2.asm",
        "simd/x86_64/jdmerge-avx2.asm",
        "simd/x86_64/jdmerge-sse2.asm",
        "simd/x86_64/jdmrgext-avx2.asm",
        "simd/x86_64/jdmrgext-sse2.asm",
        "simd/x86_64/jdsample-avx2.asm",
        "simd/x86_64/jdsample-sse2.asm",
        "simd/x86_64/jfdctflt-sse.asm",
        "simd/x86_64/jfdctfst-sse2.asm",
        "simd/x86_64/jfdctint-avx2.asm",
        "simd/x86_64/jfdctint-sse2.asm",
        "simd/x86_64/jidctflt-sse2.asm",
        "simd/x86_64/jidctfst-sse2.asm",
        "simd/x86_64/jidctint-avx2.asm",
        "simd/x86_64/jidctint-sse2.asm",
        "simd/x86_64/jidctred-sse2.asm",
        "simd/x86_64/jquantf-sse2.asm",
        "simd/x86_64/jquanti-avx2.asm",
        "simd/x86_64/jquanti-sse2.asm",
        "simd/x86_64/jsimdcpu.asm",
        "simd/nasm/jcolsamp.inc",
        "simd/nasm/jdct.inc",
        "simd/nasm/jsimdcfg.inc",
        "simd/nasm/jsimdcfg.inc.h",
        "simd/nasm/jsimdext.inc",
    ],
    outs = [
        "simd/x86_64/jccolor-avx2.obj",
        "simd/x86_64/jccolor-sse2.obj",
        "simd/x86_64/jcgray-avx2.obj",
        "simd/x86_64/jcgray-sse2.obj",
        "simd/x86_64/jchuff-sse2.obj",
        "simd/x86_64/jcphuff-sse2.obj",
        "simd/x86_64/jcsample-avx2.obj",
        "simd/x86_64/jcsample-sse2.obj",
        "simd/x86_64/jdcolor-avx2.obj",
        "simd/x86_64/jdcolor-sse2.obj",
        "simd/x86_64/jdmerge-avx2.obj",
        "simd/x86_64/jdmerge-sse2.obj",
        "simd/x86_64/jdsample-avx2.obj",
        "simd/x86_64/jdsample-sse2.obj",
        "simd/x86_64/jfdctflt-sse.obj",
        "simd/x86_64/jfdctfst-sse2.obj",
        "simd/x86_64/jfdctint-avx2.obj",
        "simd/x86_64/jfdctint-sse2.obj",
        "simd/x86_64/jidctflt-sse2.obj",
        "simd/x86_64/jidctfst-sse2.obj",
        "simd/x86_64/jidctint-avx2.obj",
        "simd/x86_64/jidctint-sse2.obj",
        "simd/x86_64/jidctred-sse2.obj",
        "simd/x86_64/jquantf-sse2.obj",
        "simd/x86_64/jquanti-avx2.obj",
        "simd/x86_64/jquanti-sse2.obj",
        "simd/x86_64/jsimdcpu.obj",
    ],
    cmd = "for out in $(OUTS); do\n" +
          "  $(location @nasm//:nasm) -fwin64 -DWIN64 -D__x86_64__" +
          "    -I $$(dirname $(location simd/x86_64/jccolext-sse2.asm))/" +
          "    -I $$(dirname $(location simd/nasm/jdct.inc))/" +
          "    -I $$(dirname $(location simd/nasm/jdct.inc))/../../win/" +
          "    -o $$out" +
          "    $$(dirname $(location simd/x86_64/jccolext-sse2.asm))/$$(basename $${out%.obj}.asm)\n" +
          "done",
    tools = ["@nasm"],
)

cc_library(
    name = "simd_none",
    srcs = [
        "jconfig.h",
        "jconfigint.h",
        "src/jchuff.h",
        "src/jdct.h",
        "src/jerror.h",
        "src/jinclude.h",
        "src/jmorecfg.h",
        "src/jpegint.h",
        "src/jpeglib.h",
        "src/jsimd.h",
        "src/jsimddct.h",
    ],
    copts = libjpegturbo_copts,
)

expand_template(
    name = "jversion",
    out = "jversion.h",
    substitutions = {
        "@COPYRIGHT_YEAR@": "1991-2022",
    },
    template = "src/jversion.h.in",
)

JCONFIG_COMMON_SUBSTITUTIONS = {
    "@JPEG_LIB_VERSION@": "80",
    "@VERSION@": "3.1.2",
    "@LIBJPEG_TURBO_VERSION_NUMBER@": "3001002",
    "#cmakedefine C_ARITH_CODING_SUPPORTED 1": "#define C_ARITH_CODING_SUPPORTED 1",
    "#cmakedefine D_ARITH_CODING_SUPPORTED 1": "#define D_ARITH_CODING_SUPPORTED 1",
    "#cmakedefine RIGHT_SHIFT_IS_UNSIGNED 1": "",
}

JCONFIG_SIMD_SUBSTITUTIONS = {
    "#cmakedefine WITH_SIMD 1": "#define WITH_SIMD 1",
}

JCONFIG_NOSIMD_SUBSTITUTIONS = {
    "#cmakedefine WITH_SIMD 1": "",
}

JCONFIG_SIMD_SUBSTITUTIONS.update(JCONFIG_COMMON_SUBSTITUTIONS)

JCONFIG_NOSIMD_SUBSTITUTIONS.update(JCONFIG_COMMON_SUBSTITUTIONS)

expand_template(
    name = "jconfig_nosimd",
    out = "jconfig_nosimd.h",
    substitutions = JCONFIG_NOSIMD_SUBSTITUTIONS,
    template = "src/jconfig.h.in",
)

expand_template(
    name = "jconfig_simd",
    out = "jconfig_simd.h",
    substitutions = JCONFIG_SIMD_SUBSTITUTIONS,
    template = "src/jconfig.h.in",
)

JCONFIGINT_COMMON_SUBSTITUTIONS = {
    "@BUILD@": "20250929",
    "@VERSION@": "3.1.2",
    "@CMAKE_PROJECT_NAME@": "libjpeg-turbo",
    "#undef inline": "",
    "#cmakedefine HAVE_INTRIN_H": "",
    "#cmakedefine C_ARITH_CODING_SUPPORTED 1": "#define C_ARITH_CODING_SUPPORTED 1",
    "#cmakedefine D_ARITH_CODING_SUPPORTED 1": "#define D_ARITH_CODING_SUPPORTED 1",
    "#cmakedefine RIGHT_SHIFT_IS_UNSIGNED 1": "",
}

JCONFIGINT_NOWIN_SUBSTITUTIONS = {
    "#cmakedefine HAVE_BUILTIN_CTZL": "#define HAVE_BUILTIN_CTZL",
    "@INLINE@": "inline __attribute__((always_inline))",
    "@HIDDEN@": "__attribute__((visibility(\"hidden\")))",
    "#define SIZEOF_SIZE_T  @SIZE_T@": "#if (__WORDSIZE==64 && !defined(__native_client__))\n" +
                                       "#define SIZEOF_SIZE_T 8\n" +
                                       "#else\n" +
                                       "#define SIZEOF_SIZE_T 4\n" +
                                       "#endif\n",
    "@THREAD_LOCAL@": "__thread",
}

JCONFIGINT_WIN_SUBSTITUTIONS = {
    "#cmakedefine HAVE_BUILTIN_CTZL": "",
    "#define INLINE  @INLINE@": "#if defined(__GNUC__)\n" +
                                "#define INLINE inline __attribute__((always_inline))\n" +
                                "#elif defined(_MSC_VER)\n" +
                                "#define INLINE __forceinline\n" +
                                "#else\n" +
                                "#define INLINE\n" +
                                "#endif\n",
    "#define SIZEOF_SIZE_T  @SIZE_T@": "#if defined(_WIN64)\n" +
                                       "#define SIZEOF_SIZE_T 8\n" +
                                       "#else\n" +
                                       "#define SIZEOF_SIZE_T 4\n" +
                                       "#endif\n",
    "@HIDDEN@": "",
    "@THREAD_LOCAL@": "__declspec(thread)",
}

JCONFIGINT_NOWIN_SUBSTITUTIONS.update(JCONFIGINT_COMMON_SUBSTITUTIONS)

JCONFIGINT_WIN_SUBSTITUTIONS.update(JCONFIGINT_COMMON_SUBSTITUTIONS)

JCONFIGINT_NOWIN_NOSIMD_SUBSTITUTIONS = {}
JCONFIGINT_NOWIN_NOSIMD_SUBSTITUTIONS.update(JCONFIGINT_NOWIN_SUBSTITUTIONS)
JCONFIGINT_NOWIN_NOSIMD_SUBSTITUTIONS.update({
    "#cmakedefine WITH_SIMD 1": "",
})

JCONFIGINT_NOWIN_SIMD_SUBSTITUTIONS = {}
JCONFIGINT_NOWIN_SIMD_SUBSTITUTIONS.update(JCONFIGINT_NOWIN_SUBSTITUTIONS)
JCONFIGINT_NOWIN_SIMD_SUBSTITUTIONS.update({
    "#cmakedefine WITH_SIMD 1": "#define WITH_SIMD 1",
})

JCONFIGINT_WIN_SIMD_SUBSTITUTIONS = {}
JCONFIGINT_WIN_SIMD_SUBSTITUTIONS.update(JCONFIGINT_WIN_SUBSTITUTIONS)
JCONFIGINT_WIN_SIMD_SUBSTITUTIONS.update({
    "#cmakedefine WITH_SIMD 1": "#define WITH_SIMD 1",
})

expand_template(
    name = "jconfigint_nowin_nosimd",
    out = "jconfigint_nowin_nosimd.h",
    substitutions = JCONFIGINT_NOWIN_NOSIMD_SUBSTITUTIONS,
    template = "src/jconfigint.h.in",
)

expand_template(
    name = "jconfigint_nowin_simd",
    out = "jconfigint_nowin_simd.h",
    substitutions = JCONFIGINT_NOWIN_SIMD_SUBSTITUTIONS,
    template = "src/jconfigint.h.in",
)

expand_template(
    name = "jconfigint_win",
    out = "jconfigint_win.h",
    substitutions = JCONFIGINT_WIN_SIMD_SUBSTITUTIONS,
    template = "src/jconfigint.h.in",
)

genrule(
    name = "configure",
    srcs = [
        "jconfig_nosimd.h",
        "jconfig_simd.h",
    ],
    outs = ["jconfig.h"],
    cmd = select({
        ":windows": "cp $(location jconfig_simd.h) $@",
        ":k8": "cp $(location jconfig_simd.h) $@",
        ":armeabi-v7a": "cp $(location jconfig_simd.h) $@",
        ":arm64-v8a": "cp $(location jconfig_simd.h) $@",
        ":linux_ppc64le": "cp $(location jconfig_simd.h) $@",
        "//conditions:default": "cp $(location jconfig_nosimd.h) $@",
    }),
)

genrule(
    name = "configure_internal",
    srcs = [
        "jconfigint_win.h",
        "jconfigint_nowin_nosimd.h",
        "jconfigint_nowin_simd.h",
    ],
    outs = ["jconfigint.h"],
    cmd = select({
        ":windows": "cp $(location jconfigint_win.h) $@",
        ":k8": "cp $(location jconfigint_nowin_simd.h) $@",
        ":armeabi-v7a": "cp $(location jconfigint_nowin_simd.h) $@",
        ":arm64-v8a": "cp $(location jconfigint_nowin_simd.h) $@",
        ":linux_ppc64le": "cp $(location jconfigint_nowin_simd.h) $@",
        "//conditions:default": "cp $(location jconfigint_nowin_nosimd.h) $@",
    }),
)

string_flag(
    name = "noasm",
    build_setting_default = "no",
)

config_setting(
    name = "nosimd",
    flag_values = {":noasm": "yes"},
)

config_setting(
    name = "k8",
    flag_values = {":noasm": "no"},
    constraint_values = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
    ]
)

config_setting(
    name = "android",
    constraint_values = [
        "@platforms//os:android",
    ],
)

config_setting(
    name = "armeabi-v7a",
    flag_values = {":noasm": "no"},
    constraint_values = [
        "@platforms//cpu:armv7",
    ],
)

config_setting(
    name = "arm64-v8a",
    flag_values = {":noasm": "no"},
    constraint_values = [
        "@platforms//cpu:arm64",
    ],
)

config_setting(
    name = "windows",
    flag_values = {":noasm": "no"},
    constraint_values = [
        "@platforms//os:windows",
        "@platforms//cpu:x86_64",
    ],
)

config_setting(
    name = "linux_ppc64le",
    flag_values = {":noasm": "no"},
    constraint_values = [
        "@platforms//os:linux",
        "@platforms//cpu:ppc64le",
    ],
)
